﻿using System;
using System.Data;
using System.Collections.Generic;
using HsTec.Mobile.Green.Model;
using HsTec.Mobile.Green.Util;
using System.Data.SqlClient;

namespace HsTec.Mobile.Green.BLL
{
    /// <summary>
    /// 业务逻辑类FixedRatio 的摘要说明。
    /// </summary>
    public class FixedRatioBLL
    {
        private readonly HsTec.Mobile.Green.DAL.FixedRatioDAL dal = new HsTec.Mobile.Green.DAL.FixedRatioDAL();
        public FixedRatioBLL()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string FR_IDs)
        {
            return dal.Exists(FR_IDs);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.FixedRatio model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(HsTec.Mobile.Green.Model.FixedRatio model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string FR_IDs)
        {

            dal.Delete(FR_IDs);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.FixedRatio GetModel(int FR_ID)
        {

            return dal.GetModel(FR_ID);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HsTec.Mobile.Green.Model.FixedRatio> GetModelList(string strWhere)
        {
            DataView ds = dal.GetList(strWhere);
            return DataTableToList(ds.Table);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<HsTec.Mobile.Green.Model.FixedRatio> DataTableToList(DataTable dt)
        {
            List<HsTec.Mobile.Green.Model.FixedRatio> modelList = new List<HsTec.Mobile.Green.Model.FixedRatio>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                HsTec.Mobile.Green.Model.FixedRatio model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new HsTec.Mobile.Green.Model.FixedRatio();
                    if (dt.Rows[n]["FR_ID"].ToString() != "")
                    {
                        model.FR_ID = int.Parse(dt.Rows[n]["FR_ID"].ToString());
                    }
                    model.Years = dt.Rows[n]["Years"].ToString();
                    if (dt.Rows[n]["MonthRatio"].ToString() != "")
                    {
                        model.MonthRatio = decimal.Parse(dt.Rows[n]["MonthRatio"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetAllList()
        {
            return GetList("");
        }

        #endregion  成员方法

        public DataView GetFixedRatioList(string years, int iType)
        {
            return dal.GetFixedRatioList(years, iType);
        }

        public int SetFixedRatio(Dictionary<string, string> dic)
        {
            SQLHelper.myconn.Open();
            SqlCommand command = SQLHelper.myconn.CreateCommand();
            SqlTransaction transaction;
            transaction = SQLHelper.myconn.BeginTransaction();
            command.Connection = SQLHelper.myconn;
            command.Transaction = transaction;
            try
            {
                foreach (string key in dic.Keys)
                {
                    if (String.IsNullOrEmpty(dic[key]))
                        break;
                    command.CommandText = "delete from tb_FixedRatio where Years='" + key + "'";
                    command.ExecuteNonQuery();
                    command.CommandText = "insert into tb_FixedRatio(Years,MonthRatio) values('" + key + "','" + dic[key] + "')";
                    command.ExecuteNonQuery();
                }
                transaction.Commit();
                return 1;
            }
            catch
            {
                transaction.Rollback();
                return 0;
            }
            finally
            {
                transaction.Dispose();
                SQLHelper.myconn.Close();
            }
        }
    }
}

